home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************
- *
- * Copyright (c) 1993 Silicon Graphics, Inc.
- * All Rights Reserved
- *
- * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF SGI
- *
- * The copyright notice above does not evidence any actual of intended
- * publication of such source code, and is an unpublished work by Silicon
- * Graphics, Inc. This material contains CONFIDENTIAL INFORMATION that is
- * the property of Silicon Graphics, Inc. Any use, duplication or
- * disclosure not specifically authorized by Silicon Graphics is strictly
- * prohibited.
- *
- * RESTRICTED RIGHTS LEGEND:
- *
- * Use, duplication or disclosure by the Government is subject to
- * restrictions as set forth in subdivision (c)(1)(ii) of the Rights in
- * Technical Data and Computer Software clause at DFARS 52.227-7013,
- * and/or in similar or successor clauses in the FAR, DOD or NASA FAR
- * Supplement. Unpublished - rights reserved under the Copyright Laws of
- * the United States. Contractor is SILICON GRAPHICS, INC., 2011 N.
- * Shoreline Blvd., Mountain View, CA 94039-7311
- **************************************************************************
- *
- * File: impzoom.c
- *
- * Description: Zooms an SGI Image file. The type of zooming can be
- * specified. The default is impulse zooming (i.e. replicative/
- * decimation).
- *
- * Usage: impzoom [-f filter] [-b blur] [-l] xfact yfact inimage outimage
- *
- * If -l is specified the xfact and yfact are interpreted as the
- * actual size of the zoomed image in pixels. The default is to
- * treat xfact and yfact as zoom factors.
- *
- **************************************************************************/
-
-
- #ident "$Revision: 1.3 $"
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include "imp.h"
-
-
- /* Row read function client data structure */
-
- typedef struct {
- int channel;
- IMPImage *image;
- } RowInfo;
-
-
- /* Local functions */
-
- static void usage(char *pname);
- static int readRow(short *buffer, ushort_t row, void *clientData);
-
-
- /**************************************************************************
- *
- * Function: main
- *
- * Description: Program entry point
- *
- * Parameters:
- * argc (I) - number of command line arguments
- * argv (I) - command line arguments
- *
- * Return: 0 if no errors. 1 if errors.
- *
- **************************************************************************/
-
- int main(int argc, char *argv[])
- {
- char *pname, *inName, *outName;
- char *xfactptr, *yfactptr;
- int ch, inNumChan, literal = 0;
- register int row;
- ushort_t srcXSize, srcYSize, dstXSize, dstYSize;
- short *buffer;
- IMPFilterType filterType = IMPImpulse;
- IMPImage *inImage, *outImage;
- float blur = 1.0;
- IMPZoom *zoom;
- RowInfo rowInfo;
-
- /*
- * Extract the program name
- */
- pname = ((pname = strrchr(argv[0], '/')) == NULL) ? argv[0]: pname + 1;
-
- /*
- * Handle command line options
- */
- while ((ch = getopt(argc, argv, "f:b:l")) != EOF) {
- switch (ch) {
- case 'f':
- if (!strcasecmp(optarg, "IMPULSE"))
- filterType = IMPImpulse;
- else if (!strcasecmp(optarg, "BOX"))
- filterType = IMPBox;
- else if (!strcasecmp(optarg, "TRIANGLE"))
- filterType = IMPTriangle;
- else if (!strcasecmp(optarg, "QUADRATIC"))
- filterType = IMPQuadratic;
- else if (!strcasecmp(optarg, "MITCHELL"))
- filterType = IMPMitchell;
- else if (!strcasecmp(optarg, "GAUSSIAN"))
- filterType = IMPGaussian;
- else
- fprintf(stderr,
- "%s: unrecognized filter type %s, IMPULSE used\n",
- pname, optarg);
- break;
- case 'b':
- blur = atof(optarg);
- break;
- case 'l':
- literal = 1;
- break;
- case '?':
- default:
- usage(pname);
- exit(1);
- }
- }
-
- /*
- * Handle required command line arguments
- */
- if (argc - optind < 4) {
- usage(pname);
- exit(1);
- }
- xfactptr = argv[optind++];
- yfactptr = argv[optind++];
- inName = argv[optind++];
- outName = argv[optind];
-
- /*
- * Open the original image
- */
- if ((inImage = impOpen(inName, "r")) == NULL ) {
- impPerror(pname);
- exit(1);
- }
- srcXSize = impXSize(inImage);
- srcYSize = impYSize(inImage);
- inNumChan = impNumChannels(inImage);
-
- /*
- * Determine the zoomed image dimensions
- */
- if (literal) {
- dstXSize = atoi(xfactptr);
- dstYSize = atoi(yfactptr);
- } else {
- dstXSize = srcXSize * atof(xfactptr) + 0.5;
- dstYSize = srcYSize * atof(yfactptr) + 0.5;
- }
- if (!dstXSize || !dstYSize) {
- fprintf(stderr, "%s: zoom size %d by %d is invalid\n",
- pname, dstXSize, dstYSize);
- exit(1);
- }
-
- /*
- * Open the zoomed image
- */
- if ((outImage = impOpen(outName, "w",
- impMakeRasterType(impRasterEncoding(inImage),
- impRasterBPP(inImage)),
- impDimension(inImage),
- dstXSize, dstYSize, inNumChan,
- impImageType(inImage),
- impName(inImage))) == NULL) {
- impPerror(pname);
- exit(1);
- }
-
- /*
- * Create the zoom operator
- */
- if ((zoom = impCreateZoom(srcXSize, srcYSize, dstXSize, dstYSize,
- impMinValue(inImage), impMaxValue(inImage),
- readRow, 1, filterType, blur)) == NULL) {
- impPerror(pname);
- exit(1);
- }
-
- /*
- * Zoom the original image
- */
- buffer = (short*)malloc(dstXSize * sizeof(short));
- rowInfo.image = inImage;
- for (rowInfo.channel = 0; rowInfo.channel < inNumChan; rowInfo.channel++) {
- /*
- * Reset the zoom operator between channels
- */
- impResetZoom(zoom);
-
- for (row = 0; row < dstYSize; row++) {
- if (impZoomRow(zoom, buffer, row, &rowInfo) < 0) {
- impPerror(pname);
- exit(1);
- }
- if (impWriteRow(outImage, buffer, row, rowInfo.channel) < 0) {
- impPerror(pname);
- exit(1);
- }
- }
- }
- free(buffer);
-
- /*
- * Destroy the zoom operator
- */
- impDestroyZoom(zoom);
-
- /*
- * Close up all images and free storage
- */
- if (impClose(inImage) < 0) {
- impPerror(pname);
- exit(1);
- }
- if (impClose(outImage) < 0) {
- impPerror(pname);
- exit(1);
- }
-
- return 0;
- }
-
-
- /*
- =======================================================================
- LOCAL FUNCTIONS
- =======================================================================
- */
-
-
- /**************************************************************************
- *
- * Function: usage
- *
- * Description: Prints program usage message.
- *
- * Parameters:
- * pname (I) - program name
- *
- * Return: none
- *
- **************************************************************************/
-
- static void usage(char *pname)
- {
- fprintf(stderr,
- "Usage: %s [-f filter] [-b blur] [-l] xfact yfact inimage outimage\n",
- pname);
- }
-
-
- /**************************************************************************
- *
- * Function: readRow
- *
- * Description: Called by the zooming function to read a row of data.
- *
- * Parameters:
- * buffer (I) - buffer to fill with row data
- * row (I) - row number to read
- * clientData (I) - pointer to client data.
- *
- * Return: -1 if errors, 0 or greater if no errors.
- *
- **************************************************************************/
-
- static int readRow(short *buffer, ushort_t row, void *clientData)
- {
- register RowInfo *rowInfo = (RowInfo*)clientData;
-
- return impReadRow(rowInfo->image, buffer, row, rowInfo->channel);
- }
-